<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Staticblock extends Admin_Controller {
    function Staticblock(){
        parent::Admin_Controller();
        $this->load->model('staticblock_model');
        $this->load->library('session');
        $this->load->helper('url');
        $this->bep_assets->load_asset('master');
    }
    public function index()
    {
        $staticblockList = $this->staticblock_model->getStaticBlockList();
        $data['staticblock_list'] = $staticblockList;
        $data['page'] = $this->config->item('backendpro_template_admin') . "list";
        $data['module'] ='staticblock';
        $this->load->view($this->_container,$data);
    }
    public function add()
    {
        $data['gallery_list'] = $this->staticblock_model->getGalleryList();
        $data['page'] = $this->config->item('backendpro_template_admin') . "add";
        $data['module'] ='staticblock';
        $this->load->view($this->_container,$data);
    }

    public function save()
    {
        $newstaticblock = $_POST;

        $this->staticblock_model->addNewStaticBlock($newstaticblock);
        redirect('staticblock');
    }

    public function edit($id)
    {
        if($this->uri->segments[3]> 0){
            $id = $this->uri->segments[3];
            $data = array();
            $gallery = $this->gallery_model->getGalleryById($id);
            $photosPath = $_SERVER['DOCUMENT_ROOT'];
            $photosDir = $photosPath.'/photos/';

            $data['gallery'] = $gallery;
            $data['photosDir'] = $photosDir;

            //$this->load->view('admin/gallery/addcomment',$data);

            $data['page'] = $this->config->item('backendpro_template_admin') . "addcomment";
            $data['module'] ='gallery';
            $this->load->view($this->_container,$data);
        }else{
            redirect('404_override');
        }
    }
    public function saveGallery()
    {
        $galleryArrayToDb = array();
        $galleryArray = $_POST;
        foreach($galleryArray['Gallery'] as $key => $photo)
        {
            $photo['id'] = $key;
            $galleryArrayToDb[] = $photo;
        }
        $res = $this->gallery_model->updateGalleryPhoto($galleryArrayToDb);
        $this->session->set_flashdata('message', 'Добавлена новая галерея');
        redirect('gallery/list');
    }

    public function deletecat()
    {

        if(isset($this->uri->segments[3]) && $this->uri->segments[3] > 0)
        {
            $galleryId = $this->uri->segments[3];
            $updatedGalleryName = $this->gallery_model->deleteGalleryById($galleryId);
            $this->session->set_flashdata('message', 'Удалена галерея '.$updatedGalleryName);
            redirect('gallery');
        }
        redirect('404');
    }

    private function _translater($string){
        $converter = array(
            'а' => 'a',   'б' => 'b',   'в' => 'v',
            'г' => 'g',   'д' => 'd',   'е' => 'e',
            'ё' => 'e',   'ж' => 'zh',  'з' => 'z',
            'и' => 'i',   'й' => 'y',   'к' => 'k',
            'л' => 'l',   'м' => 'm',   'н' => 'n',
            'о' => 'o',   'п' => 'p',   'р' => 'r',
            'с' => 's',   'т' => 't',   'у' => 'u',
            'ф' => 'f',   'х' => 'h',   'ц' => 'c',
            'ч' => 'ch',  'ш' => 'sh',  'щ' => 'sch',
            'ь' => "'",  'ы' => 'y',   'ъ' => "'",
            'э' => 'e',   'ю' => 'yu',  'я' => 'ya',

            'А' => 'A',   'Б' => 'B',   'В' => 'V',
            'Г' => 'G',   'Д' => 'D',   'Е' => 'E',
            'Ё' => 'E',   'Ж' => 'Zh',  'З' => 'Z',
            'И' => 'I',   'Й' => 'Y',   'К' => 'K',
            'Л' => 'L',   'М' => 'M',   'Н' => 'N',
            'О' => 'O',   'П' => 'P',   'Р' => 'R',
            'С' => 'S',   'Т' => 'T',   'У' => 'U',
            'Ф' => 'F',   'Х' => 'H',   'Ц' => 'C',
            'Ч' => 'Ch',  'Ш' => 'Sh',  'Щ' => 'Sch',
            'Ь' => "'",  'Ы' => 'Y',   'Ъ' => "'",
            'Э' => 'E',   'Ю' => 'Yu',  'Я' => 'Ya',
            ' ' => '',
        );
        return strtr($string, $converter);
    }

    public function galleryList()
    {
        $galleryList = $this->gallery_model->getGalleryListWithPhoto();
        $data['gallery_list'] = $galleryList;
        $this->load->view('admin/head');
        $this->load->view('admin/admin_menu');
        $this->load->view('admin/gallery/list',$data);
        $this->load->view('admin/footer');
    }
    public function galleryActive()
    {
        if(isset($this->uri->segments[4]) && $this->uri->segments[4] > 0)
        {
            $galleryId = $this->uri->segments[4];
            $result = $this->gallery_model->changeStatus($galleryId);
            redirect('admin/gallery/list');
        }
    }
}//end class
?>